package cn.gbase.jiangsu.data.transfer.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.service.IdEntityService;

import cn.gbase.jiangsu.data.transfer.bean.ProductComment;
import cn.gbase.jiangsu.data.transfer.bean.Supplier;
import cn.gbase.jiangsu.data.transfer.bean.SupplierUser;
import cn.gbase.jiangsu.data.transfer.util.PropertyFilter;
import cn.gbase.jiangsu.data.transfer.util.SystemContext;

@IocBean(args = { "refer:dao" })
@At("/productComment")
public class ProductCommentAction extends IdEntityService<Supplier> {

	public ProductCommentAction() {
		super();
	}

	public ProductCommentAction(Dao dao) {
		super(dao);
	}

	/*@At("/listProduct")
	@Ok("jsp:/comment/listProduct")
	public QueryResult listProduct(@Param("currentPage") int currentPage, @Param("id") String id,
			@Param("keyword") String keyword, @Param("nodeId") String nodeId,
			@Param("selectedNodeName") String selectedNodeName, @Param("passStatus") String passStatus,
			@Param("supplierId") String supplierId, HttpServletRequest request) {

		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("id", id);
		itemMap.put("keyword", keyword);
		itemMap.put("nodeId", nodeId);
		itemMap.put("selectedNodeName", selectedNodeName);
		itemMap.put("passStatus", passStatus);
		itemMap.put("supplierId", supplierId);
		request.setAttribute("formdata", itemMap);

		// 商品分类
		String json = productService.getTreeJsonDataForZTree(1);
		request.setAttribute("treedata", json);

		// 供应商列表
		List<Supplier> supplierList = supplierService.getSupplierList();
		request.setAttribute("supplierList", supplierList);

		return productService.getProductList(currentPage, id, keyword, nodeId, passStatus, supplierId);

	}
	*/
	/**
	 * 分页查询
	 * @param pageNum 第几页
	 * @param numPerPage  每页显示多少条
	 * @return
	 */
	@At("/listComment")
	@Ok("jsp:/comment/listComment")
	public Object list(@Param("p") int pageNum, @Param("numPerPage") int numPerPage, HttpServletRequest request,
			@Attr(scope = Scope.SESSION, value = SystemContext.SUPPLIERUSER) SupplierUser supplierUser) {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		Cnd cnd = PropertyFilter.toCnd(filters, "id", "DESC");
		//cnd.and("create_user_id", "=", user.getUserId() + "");
		Pager pager = this.dao().createPager((pageNum < 1) ? 1 : pageNum,
				(numPerPage < 1) ? SystemContext.PAGE_SIZE : numPerPage);
		List<ProductComment> list = this.dao().query(ProductComment.class, cnd, pager);
		if (pager != null) {
			pager.setRecordCount(this.dao().count(ProductComment.class, cnd));
		}
		return new QueryResult(list, pager);
	}

	/**
	 * 回复信息预处理
	 * @param commentId  评论ID
	 * @return
	 */
	@At("/preCommentReply/?")
	@Ok("jsp:/comment/commentReply")
	public Object preCommentReply(Integer commentId) {
		ProductComment pc = this.dao().fetch(ProductComment.class, commentId);
		return pc;
	}

	/**
	 * 回复信息保存
	 * @param commentId  评论ID
	 * @return
	 */
	@At("/saveCommentReply")
	@Ok("jsp:/comment/commentReply")
	public Object saveCommentReply(@Param("..") ProductComment productComment, HttpServletRequest request) {
		this.dao().updateIgnoreNull(productComment);
		request.setAttribute("message", "已回复");
		return productComment;
	}
}
